import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
      total: 0, //总条数
      page: 1, //当前页
      size: 10, //每页显示条数
      search: '',
      auditModel: false,
      auditObj: {
      	initiationAuditMsg:""
      }, //审核对象
      FileUpload: [],
      projectList: [],
      initiationAuditMsg:false,//判断不通过内容是否输入
      curIndex:-1,//选中下标
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
    //初始化分页信息
    //this.searchTodo();
	},
	mounted() {},
	methods: { //写函数方法
		//给选中的列加背景色
		choosed:function(index){
			this.curIndex=index;
		},
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
      this.searchTodo();
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
    auditModelShow: function(projectId) {
      this.auditObj = {};
      this.FileUpload = [];
      this.auditObj.id = projectId;
      this.auditModel = true;
    },
    auditData: function() {
      let auditObj = this.auditObj;
      this.initiationAuditMsg=true;
      if (auditObj.initiationAuditMsg == null || auditObj.initiationAuditMsg.length == 0 || auditObj.initiationAuditMsg.length > 100) {
        return;
      }
      this.auditProject(auditObj.id, 3, auditObj.initiationAuditMsg, auditObj.initiationAuditEnclosureUrl);
      this.auditModel = false;
      this.initiationAuditMsg=false;
    },
    //关闭模态框
    closeModel: function() {
      this.auditModel = false;
      this.initiationAuditMsg=false;
    },
    //预览
    preview: function(url,msg) {
      if (url == null || url == "") {
        if (msg == null || msg == "") {
          msg = "附件";
        }
        layer.msg("暂无" + msg + "！");
      } else {
        window.open(this.urls.baseUrl + url);
      }
    },
    //审核状态
    //立项审核状态（1：待审核；2：审核通过；3：审核不通过）
    auditStatus: function(status) {
      if (status != null) {
        switch (status) {
          case 0:
            return "暂存";
          case 1:
            return "待审核";
          case 2:
            return "通过";
          case 3:
            return "不通过";
        }
      }
    },
    //删除附件
    deleteEnclosure: function() {
      this.FileUpload = [];
      this.auditObj.initiationAuditEnclosureUrl = "";
    },
    /**文件上传js**/
    idFrontImg: function(event) {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if (res.success) {
            let fileInfo = res.data[0];
            that.auditObj.initiationAuditEnclosureUrl = fileInfo.url;
            that.FileUpload = [that.getFileName(fileInfo.url)];
          }
        })
    },
    //搜索数据列表
    searchTodo: function() {
      let that = this;
      that.$post(
        that.urls.getProjectInitiationPageInfo,
        {"pageNum": that.page,"pageSize": that.size,"search": that.search}
      )
      .then(function(res) {
        if(res.success) {
          that.projectList = res.pageInfo.list;
          that.total = res.pageInfo.total;
          that.page = res.pageInfo.pageNum;
        }
      })
    },
    //审核
    auditProject: function(id, initiationAuditStatus, initiationAuditMsg, initiationAuditEnclosureUrl) {
      if (id == null || initiationAuditStatus == null) {
        return;
      }
      let that = this;
      that.$post(
        that.urls.auditInitiationProject,
        {"id": id, "initiationAuditStatus": initiationAuditStatus, "initiationAuditMsg": initiationAuditMsg, "initiationAuditEnclosureUrl": initiationAuditEnclosureUrl}
      )
      .then(function(res) {
        let msg = "";
        if(res.success) {
          msg = '审核成功！';
          //重新渲染数据
          that.searchTodo();
        } else {
          msg = '审核失败！';
        }
        layer.msg(msg, {
          time: 3000, //3s后自动关闭
        });
      })
    },
    //生成上账
    accounting: function(id) {
      if (id == null) {
        return;
      }
      let that = this;
      that.$post(
        that.urls.accounting,
        {"id": id}
      )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = '生成上账成功！';
            //重新渲染数据
            that.searchTodo();
          } else {
            msg = '生成上账失败！';
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
    },
    //项目立项结束
    approvalEnd: function (id) {
      if (id != null) {
        let that = this;
        that.$post(
          that.urls.approvalEnd,
          {"id": id}
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = '成功生成上账文件！';
            that.searchTodo();
          } else {
            msg = '生成上账文件失败！';
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    }
	}
}
